package com.alks.function.service.impl.chengKong;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.dto.SysUserTokenDTO;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.UserInfoUtil;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.entity.*;
import com.alks.entity.data.enums.chengkong.DefineEnum;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.chengKong.assort.*;
import com.alks.function.data.dto.procurement.PurchaseOrderDto;
import com.alks.function.data.request.chengKong.*;
import com.alks.function.data.request.procurement.LotAndMtlRequest;
import com.alks.function.mapper.chengKong.*;
import com.alks.function.mapper.procurement.MaterialClearanceMapper;
import com.alks.function.service.chengKong.PartService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PartServiceImpl implements PartService {

    @Resource
    private PartMapper partMapper;

    @Resource
    private ColorAttributeMapper colorAttributeMapper;

    @Autowired
    private MaterialClearanceMapper materialClearanceMapper;

    /**
     * 部位明细
     * @param request
     * @return
     */
    @AutoPageAop
    @Override
    public ResponseInfo getPartList(PartRequest request) {

        HashMap map =new HashMap();

        List<YfOrderPartName> partList = partMapper.getPartList(request);

        PageInfo list=new PageInfo(partList);

        map.put("partList",partList);
        map.put("total",list.getTotal());
        return ResponseInfo.ok(map);
    }

    /**
     * 部位建档
     * @param yfOrderPartName
     * @return
     */
    @Override
    public ResponseInfo getPartAdd(YfOrderPartName yfOrderPartName) {

        if (StringUtils.isEmpty(yfOrderPartName.getSeqNo())){
            return ResponseInfo.error("序号不能为空!");
        }

        String partSeqNo = partMapper.getPartSeqNo(yfOrderPartName.getSeqNo());
        if (!StringUtils.isEmpty(partSeqNo)){
            return ResponseInfo.error(partSeqNo+"序号已经存在!");
        }

        if (StringUtils.isEmpty(yfOrderPartName.getPartName())){
            return ResponseInfo.error("请输入部位名称!");
        }

        String partName = partMapper.getPartName(yfOrderPartName.getPartName());
        if (!StringUtils.isEmpty(partName)){
            return  ResponseInfo.error(partName+"部位名称已经存在!");
        }

        //获取用户名
        SysUserTokenDTO dto = UserIdThread.get();
        String userName = dto.getUserName();

        yfOrderPartName.setSysUser(userName);
        yfOrderPartName.setSysDate(new Date());

        //添加
        partMapper.addPart(yfOrderPartName);

        return ResponseInfo.ok("添加成功!");
    }

    /**
     * 部位修改
     * @param yfOrderPartName
     * @return
     */
    @Override
    public ResponseInfo getPartUpdate(YfOrderPartName yfOrderPartName) {

        //获取用户名
        SysUserTokenDTO dto = UserIdThread.get();
        String userName = dto.getUserName();

        yfOrderPartName.setSysUser(userName);
        yfOrderPartName.setSysDate(new Date());

        partMapper.updatePart(yfOrderPartName);
        return ResponseInfo.ok("修改成功!");
    }

    /**
     * 部位删除
     * @param partName
     * @return
     */
    @Override
    public ResponseInfo getPartDel(String partName) {

        partMapper.delPart(partName);
        return ResponseInfo.ok("删除成功!");
    }

    /**
     * 分类资料
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getClassification(MatterRequest request) {
        List<YfCodeDto> classificationList = partMapper.getClassificationList(request);
        return ResponseInfo.ok(classificationList);
    }

    @Resource
    private MaterialManagementMapper materialManagementMapper;

    /**
     * 材料属性列表
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getMaterialProperties(MatterRequest request) {
        HashMap map = new HashMap();
        List<YfMtlTypeDetailDto> typeDetailList = materialManagementMapper.getTypeDetailList(request);
        map.put("typeDetailList", typeDetailList);
        map.put("wide", "宽幅");
        map.put("unit", "单位");
        map.put("supplier", "供应商");
        return ResponseInfo.ok(map);
    }

    /**
     * 组合材料
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getMaterialPropertiesIn(List<MaterialRequest> request) {

        String mtlPropertyName = getString(request);

        HashMap map=new HashMap();
        map.put("mtlProperty",mtlPropertyName);

        return ResponseInfo.ok(map);
    }

    /**
     * 组合材料公共方法
     * @param request
     * @return
     */
    @NotNull
    private String getString(List<MaterialRequest> request) {

        //排序 null值放在最后
        request = request.stream()
                .filter(Objects::nonNull)
                .sorted(Comparator.comparing(MaterialRequest::getPropertySeq, Comparator.nullsLast(Comparator.naturalOrder())))
                .collect(Collectors.toList());

        StringBuilder name = new StringBuilder();
        request.stream().forEach(s->{
            if (s.getMtlProperty2() != null && !s.getMtlProperty2().isEmpty()) {
                name.append(s.getMtlProperty2().trim()).append(" ");
            }
        });

        //去除首尾空格
        String mtlPropertyName = name.toString().trim();
        return mtlPropertyName;
    }

    /**
     * 材料属性添加
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getMaterialPropertiesAdd(MaterialAddRequest request) {

      if (StringUtils.isEmpty(request.getMtlUnit())){
            return ResponseInfo.error("单位不能为空!");
        }

        for (MaterialRequest materialRequest : request.getList()) {
            if (StringUtils.isEmpty(materialRequest.getMtlProperty2())&&"Y".equals(materialRequest.getPropertyNeed())){
                return ResponseInfo.error("材料属性为红色字体为必填,不能为空!");
            }
        }

        String name = getString(request.getList());

        if (StringUtils.isEmpty(request.getTypeNo3())){
            return ResponseInfo.error("分类编码不能为空!");
        }

        //根据材料名称规格
        Integer materialByName = partMapper.getMaterialByName(name,request.getMtlSpec(), request.getMtlUnit());
        if (materialByName != null&& materialByName > 0){
            return ResponseInfo.error("材料已经存在,不可插入!");
        }

        //查询物料编码
        String mtlNo = partMapper.getMtlNo(request.getTypeNo3());
        SysUserTokenDTO dto = UserIdThread.get();
        String companyId = dto.getCompanyId();
        String userName = dto.getUserName();

        YfMtlNew yfMtlNew = new YfMtlNew();

        yfMtlNew.setMtlName(name);
        yfMtlNew.setCompanyId(companyId);
        yfMtlNew.setSysUser(userName);
        yfMtlNew.setSysDate(new Date());
        yfMtlNew.setMtlNo(mtlNo);
        yfMtlNew.setMtlSpec(request.getMtlSpec());
        yfMtlNew.setMtlUnit(request.getMtlUnit());
        yfMtlNew.setVendor(request.getVendor());
        yfMtlNew.setEngName(request.getEngName());
        //如果是IE部门设置状态为1
        if ("BFIE000".equals(dto.getDeptId())){
            yfMtlNew.setStatus("1");
        }else {
            yfMtlNew.setStatus("0");
        }
        partMapper.AddMaterialProperties(yfMtlNew);
        return ResponseInfo.ok("添加成功!");
    }

    /**
     * 材料查询
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getMaterialQueryList(MaterialQueryRequest request) {

        HashMap map = new HashMap();
        List<YfMtlNewDto> materialQueryList = partMapper.getMaterialQueryList(request);
        PageInfo list = new PageInfo(materialQueryList);

        map.put("materialQueryList", materialQueryList);
        map.put("total", list.getTotal());
        return ResponseInfo.ok(map);
    }

    /**
     * 材料查询删除
     * @param mtlNo
     * @return
     */
    @Override
    public ResponseInfo getMaterialPropertiesDel(String mtlNo) {

        //查询物料编码
        YfMtlNewDto number = partMapper.getNumber(mtlNo);
        if (number==null){
            return ResponseInfo.error("数据有误不能删除!");
        }

        SysUserTokenDTO dto = UserIdThread.get();
        String userName = dto.getUserName();
        if ("1".equals(number.getStatus())){
            return ResponseInfo.error("材料已被审核,不能删除!");
        }
        if (!userName.equals(number.getSysUser())){
            return ResponseInfo.error("非本人添加,不能删除!");
        }

        Integer count = partMapper.getYfBomMtlByMtlNo(mtlNo);
        if (count != null && count > 0) {
            return ResponseInfo.error("正式材料已被使用,不能删除!");
        }
        partMapper.delMaterialProperties(mtlNo);

        return ResponseInfo.ok("删除成功!");
    }

    /**
     * 材料查询修改
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getMaterialPropertiesUpdate(MaterialUpdateRequest request) {
        if (request==null){
            return ResponseInfo.error("参数为空不可修改!");
        }

        if (StringUtils.isEmpty(request.getMtlNo())){
            return ResponseInfo.error("缺少必要参数不可修改!");
        }

        //正则表达式 时间格式
        String timePattern = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}";
        LocalDate now = LocalDate.now();
        if (!StringUtils.isEmpty(request.getUneffectDate())&&request.getUneffectDate()!=null){
            if (!Pattern.matches(timePattern,request.getUneffectDate())){
                return ResponseInfo.error("时间格式错误!例如："+now);
            }
        }

        YfMtlNew yfMtlNew = new YfMtlNew();
        yfMtlNew.setMtlNo(request.getMtlNo());
        yfMtlNew.setVendor(request.getVendor());
        yfMtlNew.setEngName(request.getEngName());

        if (!StringUtils.isEmpty(request.getUneffectDate())&&request.getUneffectDate()!=null){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date data = format.parse(request.getUneffectDate());
                yfMtlNew.setUneffectDate(data);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        SysUserTokenDTO dto = UserIdThread.get();
        String userName = dto.getUserName();
        yfMtlNew.setSysUser(userName);
        yfMtlNew.setSysDate(new Date());

        partMapper.updateMaterialProperties(yfMtlNew);

        return ResponseInfo.ok("操作成功!"+yfMtlNew);
    }

    /**
     * 材料查询修改
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getMaterialProperties2Update(Map<String, MaterialUpdate2Request> request) {

        if (request.size()!=2){
            return ResponseInfo.error("请求参数错误!");
        }

        //拿到key
        MaterialUpdate2Request updateTop = request.get("updateTop");
        MaterialUpdate2Request updateEnd = request.get("updateEnd");

        //查询物料编码
        YfMtlNewDto number = partMapper.getNumber(updateTop.getMtlNo());
        if (number==null){
            return ResponseInfo.error("数据有误不能修改!");
        }

        SysUserTokenDTO dto = UserIdThread.get();
        String userName = dto.getUserName();
        if (!userName.equals(number.getSysUser())){
            return ResponseInfo.error("非本人添加的材料,不能修改!");
        }

        if (updateTop.isEqual(updateEnd)){
            return ResponseInfo.error("新材料信息与原材料信息一样,不能修改!");
        }

        Integer materialByName = partMapper.getMaterialByName(updateEnd.getMtlName(),updateEnd.getMtlSpec(), updateEnd.getMtlUnit());
        if (materialByName != null&& materialByName > 0){
            return ResponseInfo.error("材料库已经存在相同材料名称、规格、单位的材料,不可修改!");
        }

        //判断是否被引用，被引用不能修改
        YfBomMtlDto yfBomMtl2 = partMapper.getYfBomMtl2(updateTop.getMtlName(), updateTop.getMtlSpec(), updateTop.getMtlUnit());
        if (!ObjectUtils.isEmpty(yfBomMtl2)){
            return ResponseInfo.error("该材料已经使用,不可修改!");
        }

        YfMtlNew yfMtlNew = new YfMtlNew();
        yfMtlNew.setMtlNo(updateEnd.getMtlNo());
        yfMtlNew.setMtlName(updateEnd.getMtlName());
        yfMtlNew.setMtlSpec(updateEnd.getMtlSpec());
        yfMtlNew.setMtlUnit(updateEnd.getMtlUnit());
        yfMtlNew.setSysUser(userName);
        yfMtlNew.setSysDate(new Date());

        partMapper.updateMaterialProperties(yfMtlNew);

        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 贴合材料建档查询
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getAdhesiveMaterial(AdhesiveMaterialRequest request) {

        HashMap map = new HashMap();
        HashMap officialMaterials = getOfficialMaterials(request);
        HashMap developmentMaterials = getDevelopmentMaterials(request);
        map.put("officialMaterials",officialMaterials);
        map.put("developmentMaterials",developmentMaterials);

        return ResponseInfo.ok(map);
    }

    /**
     * 正式材料查询
     * @param request
     * @return
     */
    @NotNull
    private HashMap getOfficialMaterials(AdhesiveMaterialRequest request) {

        HashMap map = new HashMap();

        //设置分页参数
        if (request.getPageNum() == null || request.getPageSize() == null) {
            request.setPageNum(1);
            request.setPageSize(10);
        }

        PageHelper.startPage(request.getPageNum(), request.getPageSize());

        List<YfBomMtlDto> adhesiveMaterialList = partMapper.getAdhesiveMaterialList(request);

        //获取total
        PageInfo list = new PageInfo(adhesiveMaterialList);

        map.put("adhesiveMaterialList", adhesiveMaterialList);
        map.put("total", list.getTotal());

        return map;
    }

    /**
     * 开发材料查询
     * @param request
     * @return
     */
    @NotNull
    private HashMap getDevelopmentMaterials(AdhesiveMaterialRequest request) {

        HashMap map = new HashMap();

        //设置分页参数
        if (request.getPageNum2() == null || request.getPageSize2() == null) {
            request.setPageNum2(1);
            request.setPageSize2(10);
        }

        PageHelper.startPage(request.getPageNum2(), request.getPageSize2());

        List<YfBomMtlDto> developmentMaterialsList = partMapper.getDevelopmentMaterialsList(request);

        //获取total
        PageInfo list = new PageInfo(developmentMaterialsList);

        map.put("developmentMaterialsList", developmentMaterialsList);
        map.put("total", list.getTotal());

        return map;
    }

    /**
     * 贴合材料建档添加
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getCacheAdd(CacheAddRequest request) {

        if (BeanUtil.allIsEmpty(request)) {
            return ResponseInfo.error("请求参数不能为空!");
        }

        //根据编码查询是否存在
        List<String> mtlNoList = partMapper.tempMtlNo(request.getMtlNo());
        if (mtlNoList.size()>3){
            return ResponseInfo.error("相同子材料不能超过三个!");
        }

        YfSubMtlTemp2 yfSubMtlTemp2 = new YfSubMtlTemp2();
        BeanUtils.copyProperties(request,yfSubMtlTemp2);

        SysUserTokenDTO dto = UserIdThread.get();
        String companyId = dto.getCompanyId();
        yfSubMtlTemp2.setCompanyId(companyId);

        String userName = dto.getUserName();
        yfSubMtlTemp2.setSysUser(userName);

        //根据用户名获取序号
        List<BigDecimal> serialNumberList = partMapper.getSerialNumber(userName);

        //初始化num
        BigDecimal num=null;
        boolean containsAllNumbers = true;
        for (int i = 1; i <= 99; i++) {

            num =BigDecimal.valueOf(i);
            //集合中不包含跳出
            if (!serialNumberList.contains(num)) {
                containsAllNumbers = false;
                break;
            }
        }

        if (containsAllNumbers) {
            return ResponseInfo.error("已到添加上限!");
        }

        yfSubMtlTemp2.setMtlSeq(num);
        yfSubMtlTemp2.setPercent(BigDecimal.ONE);
        yfSubMtlTemp2.setPurFlag("Y");

        partMapper.addCache(yfSubMtlTemp2);

        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 组合材料
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getCompositeMaterials(List<CacheRequest> request) {

        if (request.isEmpty()|| BeanUtil.allIsEmpty(request.get(0))){
            return ResponseInfo.error("子材料不能为空!");
        }

        StringBuilder name = new StringBuilder();
        AtomicBoolean isFirst = new AtomicBoolean(true);
        request.stream().forEach(s -> {
            if (!ObjectUtils.isEmpty(s.getMtlName()) || !ObjectUtils.isEmpty(s.getMtlColor())) {
                if (isFirst.get()) {
                    if (ObjectUtils.isEmpty(s.getMtlColor())){
                        throw new ServiceErrorException("组成正式材料第一条数据材料必须有颜色!");
                    }
                    if (!ObjectUtils.isEmpty(s.getMtlName())) {
                        name.append(" ").append(s.getMtlName());
                    }
                    if (!ObjectUtils.isEmpty(s.getPrdType())) {
                        name.append(" ").append(s.getPrdType());
                    }
                    isFirst.set(false);
                } else {
                    String name2="+";
                    if (!ObjectUtils.isEmpty(s.getMtlColor())) {
                        name2=name2+s.getMtlColor()+" ";
                    }
                    if (!ObjectUtils.isEmpty(s.getMtlName())) {
                        name2=name2+s.getMtlName()+" ";
                    }
                    if (!ObjectUtils.isEmpty(s.getPrdType())) {
                        name2=name2+s.getPrdType();
                    }
                    name.append(name2);
                }
            }
        });

        //去除首尾空格
        String mtlName = name.toString().trim();
        //返回数据
        HashMap map =new HashMap();
        map.put("mtlName",mtlName);
        map.put("mtlColor",request.get(0).getMtlColor());
        map.put("mtlSpec",request.get(0).getMtlSpec());
        map.put("mtlUnit",request.get(0).getMtlUnit());

        return ResponseInfo.ok(map);
    }

    /**
     * 贴合材料插入
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getCompositeMaterialsAdd(AdhesiveMaterialAddRequest request) {
        if (request == null) {
            return ResponseInfo.error("请求参数不能为空!");
        }
        SysUserTokenDTO dto = UserIdThread.get();
        String userName = dto.getUserName();
        String companyId = dto.getCompanyId();
        //贴合材料编码
        String numCode = null;
        if (!ObjectUtils.isEmpty(request.getCacheVoRequests())) {
            if (request.getCacheVoRequests().size() == 1) {
                YfBomMtl yfBomMtl = new YfBomMtl();
                BeanUtil.copyProperties(request, yfBomMtl);
                yfBomMtl.setSysUser(userName);
                yfBomMtl.setCompanyId(companyId);
                yfBomMtl.setSysDate(new Date());

                if (!ObjectUtils.isEmpty(request.getMtlColor())){
                    ColorSystemDto yfColorDetail = colorAttributeMapper.getYfColorDetail(request.getMtlColor());
                    if (ObjectUtils.isEmpty(yfColorDetail)){
                        return ResponseInfo.error("颜色输入错误!");
                    }else {
                        request.setColorCode(yfColorDetail.getColorCode());
                    }
                }

                String mtlNo = null;
                //如果颜色编码不为空拼接颜色编码
                if (request.getMtlNo().length() < Integer.parseInt(DefineEnum.FOURTEEN.getValue())) {
                    if (!StringUtils.isEmpty(request.getColorCode()) && !StringUtils.isEmpty(request.getMtlNo())) {
                        mtlNo = request.getMtlNo() + request.getColorCode();
                        yfBomMtl.setMtlNo(mtlNo);
                    } else {
                        mtlNo = request.getMtlNo() + DefineEnum.FIVE_ZERO.getValue();
                        yfBomMtl.setMtlNo(mtlNo);
                    }
                } else {
                    yfBomMtl.setMtlNo(request.getMtlNo());
                }
                numCode = yfBomMtl.getMtlNo();
                //判断加工方式
                if (ObjectUtils.isEmpty(request.getCacheVoRequests().get(0).getPrdType())) {
                    YfBomMtlDto mtlDto = partMapper.getYfBomMtl(yfBomMtl.getMtlColor(), yfBomMtl.getMtlName(), yfBomMtl.getMtlSpec(), yfBomMtl.getMtlUnit());
                    if (ObjectUtils.isEmpty(mtlDto)) {
                        YfBomMtlDto materialStyleNo = partMapper.getMaterialStyleNo(yfBomMtl.getMtlNo());
                        if (ObjectUtils.isEmpty(materialStyleNo)) {
                            //添加
                            partMapper.addCompositeMaterials(yfBomMtl);
                        }
                        getBomMaterials(request, companyId, numCode);
                    }else {
                        getBomMaterials(request, companyId, mtlDto.getMtlNo());
                    }
                }else {
                    numCode = partMapper.getMaterialMtlNo();
                    String finalNumCode = numCode;
                    yfBomMtl.setMtlNo(numCode);
                    YfBomMtlDto mtlDto = partMapper.getYfBomMtl(yfBomMtl.getMtlColor(), yfBomMtl.getMtlName(), yfBomMtl.getMtlSpec(), yfBomMtl.getMtlUnit());
                    if (ObjectUtils.isEmpty(mtlDto)) {
                        YfBomMtlDto materialStyleNo = partMapper.getMaterialStyleNo(yfBomMtl.getMtlNo());
                        if (ObjectUtils.isEmpty(materialStyleNo)) {
                            //添加
                            partMapper.addCompositeMaterials(yfBomMtl);
                            List<YfBomFitSub> list = request.getCacheVoRequests().stream().map(s -> {
                                YfBomFitSub yfBomFitSub = new YfBomFitSub();
                                BeanUtil.copyProperties(request, yfBomFitSub);
                                String mtlNo2 = null;

                                if (!ObjectUtils.isEmpty(s.getMtlColor())){
                                    ColorSystemDto yfColorDetail = colorAttributeMapper.getYfColorDetail(s.getMtlColor());
                                    if (!ObjectUtils.isEmpty(yfColorDetail)){
                                        s.setColorCode(yfColorDetail.getColorCode());
                                    }
                                }

                                //如果颜色编码不为空拼接颜色编码
                                if (s.getMtlNo().length() < Integer.parseInt(DefineEnum.FOURTEEN.getValue())) {
                                    if (!StringUtils.isEmpty(s.getColorCode()) && !StringUtils.isEmpty(s.getMtlNo())) {
                                        mtlNo2 = s.getMtlNo() + s.getColorCode();
                                        yfBomFitSub.setSubMtlNo(mtlNo2);
                                    } else {
                                        mtlNo2 = s.getMtlNo() + DefineEnum.FIVE_ZERO.getValue();
                                        yfBomFitSub.setSubMtlNo(mtlNo2);
                                    }
                                } else {
                                    yfBomFitSub.setSubMtlNo(s.getMtlNo());
                                }
                                yfBomFitSub.setCompanyId(companyId);
                                yfBomFitSub.setMtlNo(finalNumCode);
                                yfBomFitSub.setSysUser(userName);
                                yfBomFitSub.setSubMtlName(s.getMtlName());
                                yfBomFitSub.setSubMtlColor(s.getMtlColor());
                                yfBomFitSub.setSubMtlSpec(s.getMtlSpec());
                                yfBomFitSub.setSubMtlUnit(s.getMtlUnit());
                                yfBomFitSub.setPrdType(s.getPrdType());
                                yfBomFitSub.setPercent(s.getPercent());
                                yfBomFitSub.setPurFlag(s.getPurFlag());
                                yfBomFitSub.setMtlSeq(s.getMtlSeq());
                                yfBomFitSub.setVendor(s.getVendor());
                                return yfBomFitSub;
                            }).collect(Collectors.toList());
                            partMapper.addCacheToYfBomFitSub(list);
                        }
                        getBomMaterials(request, companyId, numCode);
                    }else {
                        getBomMaterials(request, companyId, mtlDto.getMtlNo());
                    }
                }
            }else {
                numCode = partMapper.getMaterialMtlNo();
                String finalNumCode = numCode;
                YfBomMtl bomMtl = new YfBomMtl();
                BeanUtil.copyProperties(request, bomMtl);
                bomMtl.setMtlNo(finalNumCode);
                bomMtl.setCompanyId(companyId);
                bomMtl.setSysUser(userName);
                bomMtl.setSysDate(new Date());
                YfBomMtlDto yfBomMtlDto = partMapper.getYfBomMtl(bomMtl.getMtlColor(), bomMtl.getMtlName(), bomMtl.getMtlSpec(), bomMtl.getMtlUnit());
                if (ObjectUtils.isEmpty(yfBomMtlDto)) {
                    YfBomMtlDto materialStyleNo = partMapper.getMaterialStyleNo(bomMtl.getMtlNo());
                    if (ObjectUtils.isEmpty(materialStyleNo)) {
                        //添加
                        partMapper.addCompositeMaterials(bomMtl);
                        List<YfBomFitSub> list = request.getCacheVoRequests().stream().map(s -> {
                            YfBomFitSub yfBomFitSub = new YfBomFitSub();
                            BeanUtil.copyProperties(request, yfBomFitSub);

                            String mtlNo = null;

                            if (!ObjectUtils.isEmpty(s.getMtlColor())){
                                ColorSystemDto yfColorDetail = colorAttributeMapper.getYfColorDetail(s.getMtlColor());
                                if (!ObjectUtils.isEmpty(yfColorDetail)){
                                    s.setColorCode(yfColorDetail.getColorCode());
                                }
                            }
                            //如果颜色编码不为空拼接颜色编码
                            if (s.getMtlNo().length() < Integer.parseInt(DefineEnum.FOURTEEN.getValue())) {
                                if (!StringUtils.isEmpty(s.getColorCode()) && !StringUtils.isEmpty(s.getMtlNo())) {
                                    mtlNo = s.getMtlNo() + s.getColorCode();
                                    yfBomFitSub.setSubMtlNo(mtlNo);
                                } else {
                                    mtlNo = s.getMtlNo() + DefineEnum.FIVE_ZERO.getValue();
                                    yfBomFitSub.setSubMtlNo(mtlNo);
                                }
                            } else {
                                mtlNo=s.getMtlNo();
                                yfBomFitSub.setSubMtlNo(s.getMtlNo());
                            }

                            //查询物料编码是否存在
                            YfBomMtlDto bomMtlDto = partMapper.getMaterialStyleNo(yfBomFitSub.getSubMtlNo());
                            YfBomMtlDto mtlDto = partMapper.getYfBomMtl(s.getMtlColor(), s.getMtlName(), s.getMtlSpec(),s.getMtlUnit());
                            if (ObjectUtils.isEmpty(mtlDto)) {
                                if (ObjectUtils.isEmpty(bomMtlDto)) {
                                    YfBomMtl bomMtl2 = new YfBomMtl();
                                    BeanUtil.copyProperties(s, bomMtl2);
                                    bomMtl2.setMtlNo(mtlNo);
                                    bomMtl2.setCompanyId(companyId);
                                    bomMtl2.setSysUser(userName);
                                    bomMtl2.setSysDate(new Date());
                                    //添加
                                    partMapper.addCompositeMaterials(bomMtl2);
                                }
                            }
                            yfBomFitSub.setCompanyId(companyId);
                            yfBomFitSub.setMtlNo(finalNumCode);
                            yfBomFitSub.setSysUser(userName);
                            yfBomFitSub.setSubMtlName(s.getMtlName());
                            yfBomFitSub.setSubMtlColor(s.getMtlColor());
                            yfBomFitSub.setSubMtlSpec(s.getMtlSpec());
                            yfBomFitSub.setSubMtlUnit(s.getMtlUnit());
                            yfBomFitSub.setPrdType(s.getPrdType());
                            yfBomFitSub.setPercent(s.getPercent());
                            yfBomFitSub.setPurFlag(s.getPurFlag());
                            yfBomFitSub.setMtlSeq(s.getMtlSeq());
                            yfBomFitSub.setVendor(s.getVendor());
                            return yfBomFitSub;
                        }).collect(Collectors.toList());
                        partMapper.addCacheToYfBomFitSub(list);
                    }
                    getBomMaterials(request, companyId, numCode);
                }else {
                    getBomMaterials(request, companyId, yfBomMtlDto.getMtlNo());
                }
            }
        }

        //删除
        partMapper.delCache(request.getIds(), userName);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * bom资料数据
     * @param request
     * @param companyId
     * @param numCode
     */
    @Nullable
    private void getBomMaterials(AdhesiveMaterialAddRequest request, String companyId, String numCode) {
        //判断partList是否存在
        if (!ObjectUtils.isEmpty(request.getPartList())){
            if (ObjectUtils.isEmpty(request.getPartList().getPartName())){
                throw new ServiceErrorException("请输入部位!");
            }
            //查询部位是否存在!
            String partName = partMapper.getPartName(request.getPartList().getPartName());
            if (StringUtils.isEmpty(partName)) {
                throw new ServiceErrorException("部位输入错误!");
            }

            //判断段是否存在
            if (!ObjectUtils.isEmpty(request.getPartList().getSegment())){
                YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
                BeanUtil.copyProperties(request, yfBomPartNew2);
                yfBomPartNew2.setMtlNo(numCode);
                yfBomPartNew2.setMtlNo2(numCode);
                yfBomPartNew2.setStyleNo(request.getPartList().getStyleNo());
                yfBomPartNew2.setStyleColor(request.getPartList().getStyleColor());
                yfBomPartNew2.setPartName(request.getPartList().getPartName());
                yfBomPartNew2.setSeqNo(request.getPartList().getSeqNo());
                yfBomPartNew2.setSegment(request.getPartList().getSegment());
                YfMtlTypeNew detail = getTypeDetail(numCode);
                if (!ObjectUtils.isEmpty(detail)){
                    if ("冲裁".equals(detail.getDrawDept())){
                        yfBomPartNew2.setDrawDept(detail.getDrawDept());
                        yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                    }
                    if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                        yfBomPartNew2.setDrawDept(detail.getDrawDept());
                        yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                    }
                }
                String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
                yfBomPartNew2.setUsageType(usageType);
                yfBomPartNew2.setSizeType("全号");
                //修改
                rAndDUsageMapper.UpdateStyleUsageDetailBySegment(yfBomPartNew2);
            }else {
                //查询型体资料颜色段
                List<String> segment = getPositionSegment(request.getPartList().getStyleNo(), request.getPartList().getStyleColor(), request.getPartList().getPartName());
                YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
                BeanUtil.copyProperties(request, yfBomPartNew2);
                //获取段
                @NotNull String[] value = getValue(segment);
                if (value[1].equals(DefineEnum.CHECKOUT_FLAG.getValue())){
                    throw new ServiceErrorException("已到段添加上限!");
                }
                yfBomPartNew2.setSegment(value[0]);
                yfBomPartNew2.setCompanyId(companyId);
                yfBomPartNew2.setMtlNo(numCode);
                yfBomPartNew2.setMtlNo2(numCode);
                yfBomPartNew2.setStyleNo(request.getPartList().getStyleNo());
                yfBomPartNew2.setStyleColor(request.getPartList().getStyleColor());
                yfBomPartNew2.setPartName(request.getPartList().getPartName());
                yfBomPartNew2.setSeqNo(request.getPartList().getSeqNo());
                YfMtlTypeNew detail = getTypeDetail(numCode);
                if (!ObjectUtils.isEmpty(detail)){
                    if ("冲裁".equals(detail.getDrawDept())){
                        yfBomPartNew2.setDrawDept(detail.getDrawDept());
                        yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                    }
                    if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                        yfBomPartNew2.setDrawDept(detail.getDrawDept());
                        yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                    }
                }
                String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
                yfBomPartNew2.setUsageType(usageType);
                yfBomPartNew2.setSizeType("全号");
                //保存
                rAndDUsageMapper.addStyleUsageDetail2(yfBomPartNew2);
            }
        }
    }

    private String getUsageType(String str){
        List<String> list = Arrays.asList("SF", "Y", "M2", "张", "米");
        if (!ObjectUtils.isEmpty(str)){
            if (list.contains(str)){
                //中间码
                return "M";
            }else {
                //基本码
                return "B";
            }
        }else {
            return "B";
        }
    }

    /**
     * 贴合材料建档列表
     * @return
     */
    @Override
    public ResponseInfo getCacheList() {

        //获取用户名
        SysUserTokenDTO dto = UserIdThread.get();
        String userName = dto.getUserName();

        //根据用户名查询列表
        List<YfSubMtlTemp2Dto> cacheList = partMapper.getCacheList(userName);
        return ResponseInfo.ok(cacheList);
    }

    /**
     * 贴合材料建档删除
     * @param ids
     * @return
     */
    @Override
    public ResponseInfo getCacheDel(List<String> ids) {

        if (ObjectUtils.isEmpty(ids)){
            return ResponseInfo.error("为空不能删除!");
        }

        //获取用户名
        SysUserTokenDTO dto = UserIdThread.get();
        String userName = dto.getUserName();

        //删除
        partMapper.delCache(ids,userName);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 贴合材料建档移动
     * @param id
     * @param choose
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getCacheMove(Integer id, Integer choose) {

        if (choose != 0 && choose != 1) {
            return ResponseInfo.error("choose参数错误!");
        }

        //获取用户名
        SysUserTokenDTO dto = UserIdThread.get();
        String userName = dto.getUserName();

        YfSubMtlTemp2Dto cache = partMapper.getCacheById(id);
        if (cache != null) {
            //根据用户名拿到list
            List<YfSubMtlTemp2Dto> cacheList = partMapper.getCacheList(userName);

            if (cacheList != null && !cacheList.isEmpty()) {

                //上移节点
                if (choose == 0) {

                    //拿到集合下标
                    int index = getCacheIndex(cache.getMtlSeq(), cacheList);
                    if (index == 0) {
                        return ResponseInfo.error("已经在第一条了,不能上移!");
                    }
                    if (index > 0) {
                        YfSubMtlTemp2Dto yfSubMtlTemp2Dto = cacheList.get(index - 1);

                        //当前节点
                        BigDecimal currentSort = cache.getMtlSeq();
                        //前一个节点
                        BigDecimal prevNodeSort = yfSubMtlTemp2Dto.getMtlSeq();

                        //交换当前节点与前一个节点
                        cache.setMtlSeq(prevNodeSort);
                        yfSubMtlTemp2Dto.setMtlSeq(currentSort);

                        //修改
                        partMapper.updateCacheById(cache.getId(), cache.getMtlSeq());
                        partMapper.updateCacheById(yfSubMtlTemp2Dto.getId(), yfSubMtlTemp2Dto.getMtlSeq());
                    }
                }

                //下移
                if (choose == 1) {

                    //拿到集合下标
                    int index = getCacheIndex(cache.getMtlSeq(), cacheList);
                    if (index == cacheList.size() - 1) {
                        return ResponseInfo.error("已经是最后一条了,不能下移！");
                    }

                    if (index >= 0 && index < cacheList.size() - 1) {

                        //拿到下一个节点的对象
                        YfSubMtlTemp2Dto yfSubMtlTemp2Dto = cacheList.get(index + 1);

                        //当前节点
                        BigDecimal currentSort = cache.getMtlSeq();

                        //下一个节点
                        BigDecimal nextNodeSort = yfSubMtlTemp2Dto.getMtlSeq();

                        //交换当前节点与下一级节点
                        cache.setMtlSeq(nextNodeSort);
                        yfSubMtlTemp2Dto.setMtlSeq(currentSort);

                        //修改
                        partMapper.updateCacheById(cache.getId(), cache.getMtlSeq());
                        partMapper.updateCacheById(yfSubMtlTemp2Dto.getId(), yfSubMtlTemp2Dto.getMtlSeq());
                    }
                }
            }
        }

        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 返回下标
     * @param mtlSeq
     * @param cacheList
     * @return
     */
    private int getCacheIndex(BigDecimal mtlSeq, List<YfSubMtlTemp2Dto> cacheList) {

        //根据集合获取当前节点的下标
        int index = cacheList.stream()
                .map(YfSubMtlTemp2Dto::getMtlSeq)
                .collect(Collectors.toList())
                .indexOf(mtlSeq);
        return index;
    }

    /**
     * 贴合材料建档修改
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getCacheUpdate(List<CacheRequest> request) {
        if (BeanUtil.allIsEmpty(request)){
            return ResponseInfo.error("参数不能为空!");
        }
        for (CacheRequest t : request) {
            //判断颜色是否存在
            if (!ObjectUtils.isEmpty(t.getMtlColor())){
                ColorSystemDto yfColorDetail = colorAttributeMapper.getYfColorDetail(t.getMtlColor());
                if (ObjectUtils.isEmpty(yfColorDetail)){
                    return ResponseInfo.error("颜色输入错误!");
                }
            }
            partMapper.updateCache(t);
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 选择生产正式材料到用量表
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getChooseCompositeMaterials(AdhesiveMaterialChooseRequest request) {

        if (ObjectUtils.isEmpty(request.getPartName())) {
            log.error("请输入部位!");
            return ResponseInfo.error("请输入部位!");
        }

        //查询部位是否存在!
        String partName = partMapper.getPartName(request.getPartName());
        if (StringUtils.isEmpty(partName)) {
            log.error("部位:{},输入错误!", request.getPartName());
            return ResponseInfo.error("部位输入错误!");
        }
        if (ObjectUtils.isEmpty(request.getMtlName())){
            return ResponseInfo.error("材料名称为空!");
        }
        if (!ObjectUtils.isEmpty(request.getMtlColor())){
            ColorSystemDto yfColorDetail = colorAttributeMapper.getYfColorDetail(request.getMtlColor());
            if (ObjectUtils.isEmpty(yfColorDetail)){
                return ResponseInfo.error("颜色输入错误!");
            }else {
                request.setColorCode(yfColorDetail.getColorCode());
            }
        }

        YfBomMtl yfBomMtl = new YfBomMtl();
        BeanUtil.copyProperties(request, yfBomMtl);

        String mtlNo = null;
        if (!ObjectUtils.isEmpty(request.getMtlNo())) {
            if (request.getMtlNo().length() < Integer.parseInt(DefineEnum.FOURTEEN.getValue())) {
                if (!ObjectUtils.isEmpty(request.getMtlColor()) && !ObjectUtils.isEmpty(request.getColorCode())) {
                    mtlNo = request.getMtlNo() + request.getColorCode();
                    yfBomMtl.setMtlNo(mtlNo);
                } else {
                    mtlNo = request.getMtlNo() + DefineEnum.FIVE_ZERO.getValue();
                    yfBomMtl.setMtlNo(mtlNo);
                }
            }
        }

        String companyId = UserIdThread.get().getCompanyId();

        //查询物料编码是否存在
        YfBomMtlDto materialStyleNo = partMapper.getMaterialStyleNo(yfBomMtl.getMtlNo());
        if (ObjectUtils.isEmpty(materialStyleNo)) {
            yfBomMtl.setCompanyId(companyId);
            yfBomMtl.setSysUser(UserIdThread.get().getUserName());
            yfBomMtl.setSysDate(new Date());

            //添加
            partMapper.addCompositeMaterials(yfBomMtl);
        }else {
            if (!materialStyleNo.getMtlName().equals(yfBomMtl.getMtlName())) {
                partMapper.updateYfBomMtl(yfBomMtl.getMtlNo(), yfBomMtl.getMtlName());
            }
        }

        //如果段不为空
        if (!ObjectUtils.isEmpty(request.getSegment())) {
            YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
            BeanUtil.copyProperties(request, yfBomPartNew2);
            yfBomPartNew2.setMtlNo(mtlNo);
            yfBomPartNew2.setMtlNo2(mtlNo);

            YfMtlTypeNew detail = getTypeDetail(mtlNo);
            if (!ObjectUtils.isEmpty(detail)){
                if ("冲裁".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
                if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
            }
            String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
            yfBomPartNew2.setUsageType(usageType);
            yfBomPartNew2.setSizeType("全号");

            //修改
            rAndDUsageMapper.UpdateStyleUsageDetailBySegment(yfBomPartNew2);
        } else {

            //查询型体资料颜色段
            List<String> segment = getPositionSegment(request.getStyleNo(), request.getStyleColor(), request.getPartName());

            YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
            BeanUtil.copyProperties(request, yfBomPartNew2);

            //获取段
            @NotNull String[] value = getValue(segment);
            if (value[1].equals(DefineEnum.CHECKOUT_FLAG.getValue())){
                return ResponseInfo.error("已到段添加上限!");
            }

            yfBomPartNew2.setSegment(value[0]);
            yfBomPartNew2.setCompanyId(companyId);
            yfBomPartNew2.setMtlNo(mtlNo);
            yfBomPartNew2.setMtlNo2(mtlNo);

            YfMtlTypeNew detail = getTypeDetail(mtlNo);
            if (!ObjectUtils.isEmpty(detail)){
                if ("冲裁".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
                if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
            }
            String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
            yfBomPartNew2.setUsageType(usageType);
            yfBomPartNew2.setSizeType("全号");

            //保存
            rAndDUsageMapper.addStyleUsageDetail2(yfBomPartNew2);
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 根据类型编号获取详情
     * @param str
     * @return
     */
    public YfMtlTypeNew getTypeDetail(String str) {
        if (str == null || str.isEmpty()) {
            return null;
        }else {
            String typeNo = str.substring(0, 2);;
            return materialManagementMapper.getByTypeNo(typeNo);
        }
    }

    /**
     * 生成段
     * @param segment
     * @return
     */
    @NotNull
    private String[] getValue(List<String> segment) {
        String[] s = new String[2];
        //初始化段
        String value = DefineEnum.SEGMENT_ZERO.getValue();
        boolean flag=true;
        for (int i = 0; i < 99; i++) {
            value=String.valueOf(i);
            if (!segment.contains(value)){
                flag=false;
                break;
            }
        }
        if (flag){
            s[1]=DefineEnum.CHECKOUT_FLAG.getValue();
            log.error("已到段添加上限!");
            return s;
        }

        s[0]=value;
        s[1]="";
        return s;
    }

    /**
     * 贴合材料处理-插入材料
     * @param request@return
     */
    @Override
    @Transactional
    public ResponseInfo getInsertMaterial(AdhesiveMaterialChooseRequest request) {
        if (ObjectUtils.isEmpty(request.getPartName())) {
            log.error("请输入部位!");
            return ResponseInfo.error("请输入部位!");
        }

        //查询部位是否存在!
        String partName = partMapper.getPartName(request.getPartName());
        if (StringUtils.isEmpty(partName)) {
            log.error("部位:{},输入错误!", request.getPartName());
            return ResponseInfo.error("部位输入错误!");
        }

        //判断是否是贴合材料
        String cache = partMapper.getCacheToYfBomFitSub(request.getMtlNo());
        if (ObjectUtils.isEmpty(cache)) {
            log.error("物料编码:{}非贴合材料不能插入!", request.getMtlNo());
            return ResponseInfo.error("非贴合材料不能插入!");
        }

        String companyId = UserIdThread.get().getCompanyId();

        //如果段不为空
        if (!ObjectUtils.isEmpty(request.getSegment())) {
            YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
            BeanUtil.copyProperties(request, yfBomPartNew2);
            yfBomPartNew2.setMtlNo2(request.getMtlNo());

            YfMtlTypeNew detail = getTypeDetail(request.getMtlNo());
            if (!ObjectUtils.isEmpty(detail)){
                if ("冲裁".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
                if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
            }
            String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
            yfBomPartNew2.setUsageType(usageType);
            yfBomPartNew2.setSizeType("全号");

            //修改
            rAndDUsageMapper.UpdateStyleUsageDetailBySegment(yfBomPartNew2);
        } else {
            //查询型体资料颜色段
            List<String> segment = getPositionSegment(request.getStyleNo(), request.getStyleColor(), request.getPartName());

            YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
            BeanUtil.copyProperties(request, yfBomPartNew2);

            //获取段
            @NotNull String[] value = getValue(segment);
            if (value[1].equals(DefineEnum.CHECKOUT_FLAG.getValue())){
                return ResponseInfo.error("已到段添加上限!");
            }

            //设置段
            yfBomPartNew2.setSegment(value[0]);
            yfBomPartNew2.setCompanyId(companyId);
            yfBomPartNew2.setMtlNo2(request.getMtlNo());

            YfMtlTypeNew detail = getTypeDetail(request.getMtlNo());
            if (!ObjectUtils.isEmpty(detail)){
                if ("冲裁".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
                if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
            }
            String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
            yfBomPartNew2.setUsageType(usageType);
            yfBomPartNew2.setSizeType("全号");
            //保存
            rAndDUsageMapper.addStyleUsageDetail2(yfBomPartNew2);
        }

        return ResponseInfo.ok("操作成功!");
    }

    @Resource
    private RAndDUsageMapper rAndDUsageMapper;

    /**
     * 选择当前行正式材料到用量表
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getChooseCurrentMaterials(AdhesiveMaterialChooseRequest request) {

        if (ObjectUtils.isEmpty(request.getPartName())) {
            log.error("请输入部位!");
            return ResponseInfo.error("请输入部位!");
        }

        //查询部位是否存在!
        String partName = partMapper.getPartName(request.getPartName());
        if (StringUtils.isEmpty(partName)) {
            log.error("部位:{},输入错误!", request.getPartName());
            return ResponseInfo.error("部位输入错误!");
        }

        //判断是否是贴合材料
        String cache = partMapper.getCacheToYfBomFitSub(request.getMtlNo());
        if (!ObjectUtils.isEmpty(cache)) {
            log.error("物料编码:{}下包含子材料,贴合材料不能在此处理!", request.getMtlNo());
            return ResponseInfo.error("贴合材料不能在此处理!");
        }

        String companyId = UserIdThread.get().getCompanyId();

        //如果段不为空
        if (!ObjectUtils.isEmpty(request.getSegment())) {
            YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
            BeanUtil.copyProperties(request, yfBomPartNew2);
            yfBomPartNew2.setMtlNo2(request.getMtlNo());

            YfMtlTypeNew detail = getTypeDetail(request.getMtlNo());
            if (!ObjectUtils.isEmpty(detail)){
                if ("冲裁".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
                if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
            }
            String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
            yfBomPartNew2.setUsageType(usageType);

            //修改
            rAndDUsageMapper.UpdateStyleUsageDetailBySegment(yfBomPartNew2);
        } else {
            //查询型体资料颜色段
            List<String> segment = getPositionSegment(request.getStyleNo(), request.getStyleColor(), request.getPartName());

            YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
            BeanUtil.copyProperties(request, yfBomPartNew2);

            //获取段
            @NotNull String[] value = getValue(segment);
            if (value[1].equals(DefineEnum.CHECKOUT_FLAG.getValue())){
                return ResponseInfo.error("已到段添加上限!");
            }

            yfBomPartNew2.setSegment(value[0]);
            yfBomPartNew2.setCompanyId(companyId);
            yfBomPartNew2.setMtlNo2(request.getMtlNo());

            YfMtlTypeNew detail = getTypeDetail(request.getMtlNo());
            if (!ObjectUtils.isEmpty(detail)){
                if ("冲裁".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
                if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
            }
            String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
            yfBomPartNew2.setUsageType(usageType);

            //保存
            rAndDUsageMapper.addStyleUsageDetail2(yfBomPartNew2);
        }

        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 查询型体资料颜色段
     * @param styleNo 型体
     * @param styleColor 颜色
     * @param partName 部位
     * @return
     */
    @Async
    public List<String> getPositionSegment(String styleNo, String styleColor, String partName){
        List<String> segment = partMapper.getPositionSegment(styleNo, styleColor, partName);
        return segment;
    }

    @Resource
    private LargeCargoUsageMapper largeCargoUsageMapper;

    /**
     * 选择材料-选择当前行材料到用料明细资料
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getChooseCurrentBomMaterials(AdhesiveMaterialChooseRequest request) {
        if (ObjectUtils.isEmpty(request.getPartName())) {
            log.error("请输入部位!");
            return ResponseInfo.error("请输入部位!");
        }

        //查询部位是否存在!
        String partName = partMapper.getPartName(request.getPartName());
        if (StringUtils.isEmpty(partName)) {
            log.error("部位:{},输入错误!", request.getPartName());
            return ResponseInfo.error("部位输入错误!");
        }

        if (!ObjectUtils.isEmpty(request.getSegment())) {
            SdBomPartNew2 bomPartNew2 = new SdBomPartNew2();
            BeanUtil.copyProperties(request,bomPartNew2);
            bomPartNew2.setMtlNo2(request.getMtlNo());

            YfMtlTypeNew detail = getTypeDetail(request.getMtlNo());
            if (!ObjectUtils.isEmpty(detail)){
                if ("冲裁".equals(detail.getDrawDept())){
                    bomPartNew2.setDrawDept(detail.getDrawDept());
                    bomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
                if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                    bomPartNew2.setDrawDept(detail.getDrawDept());
                    bomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
            }
            String usageType = getUsageType(bomPartNew2.getMtlUnit());
            bomPartNew2.setUsageType(usageType);

            //修改
            largeCargoUsageMapper.updateBomMaterial(bomPartNew2);
        }else {
            List<String> segment = largeCargoUsageMapper.getSdBomPartSegment(request.getStyleNo(), request.getStyleColor(), request.getPartName());
            @NotNull String[] value = getValue(segment);
            if (value[1].equals(DefineEnum.CHECKOUT_FLAG.getValue())){
                return ResponseInfo.error("已到段添加上限!");
            }
            SdBomPartNew2 bomPartNew2 = new SdBomPartNew2();
            BeanUtil.copyProperties(request,bomPartNew2);
            bomPartNew2.setSegment(Integer.valueOf(value[0]));
            bomPartNew2.setCompanyId(UserIdThread.get().getCompanyId());
            bomPartNew2.setMtlNo2(request.getMtlNo());
            String typeNo = getTypeNo(bomPartNew2.getMtlNo2());
            bomPartNew2.setMtlTypeNo(typeNo);

            YfMtlTypeNew detail = getTypeDetail(request.getMtlNo());
            if (!ObjectUtils.isEmpty(detail)){
                if ("冲裁".equals(detail.getDrawDept())){
                    bomPartNew2.setDrawDept(detail.getDrawDept());
                    bomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
                if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                    bomPartNew2.setDrawDept(detail.getDrawDept());
                    bomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
            }
            String usageType = getUsageType(bomPartNew2.getMtlUnit());
            bomPartNew2.setUsageType(usageType);

            //保存
            largeCargoUsageMapper.addBomMaterial(bomPartNew2);
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 拿到物料种类编号
     * @param str
     * @return
     */
    public static String getTypeNo(String str) {
        if (str == null || str.isEmpty()) {
            return null;
        } else if (str.startsWith("N3")) {
            return str.substring(0, 2);
        } else {
            return str.substring(0, 4);
        }
    }

    /**
     * 部位导出
     * @param request
     * @param response
     */
    @Override
    public void PartExport(PartRequest request, HttpServletResponse response) {
        List<YfOrderPartName> partList = partMapper.getPartList(request);
        if (ObjectUtils.isEmpty(partList)){
            throw new ServiceErrorException("查询部位明细数据为空,不能导出!");
        }
        String fileName = "部位明细_" + DateUtils.dateTimeNow() + ".xlsx";
        try (
                InputStream inputStream = new ClassPathResource("templates/部位明细导出模板.xls").getInputStream();
                OutputStream outputStream = response.getOutputStream();
                ExcelWriter excelWriter = EasyExcel.write(outputStream).excelType(ExcelTypeEnum.XLS).withTemplate(inputStream).build();
        ) {
            ExcelUtils.setResponseZF(response, fileName);
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(new FillWrapper("t", partList), fillConfig, writeSheet);
            excelWriter.finish();
        } catch (Exception e) {
            log.error("部位明细导出失败, 错误信息: {}", e.getMessage(), e);
            throw new ServiceErrorException("部位明细导出失败!");
        }
    }

    /**
     * 贴合材料还原
     * @param request
     * @return
     */
    @Override
    public ResponseInfo restoration(AdhesiveMaterialChooseRequest request) {
        if (ObjectUtils.isEmpty(request.getStyleNo())){
            return ResponseInfo.error("型体不能为空!");
        }
        if (ObjectUtils.isEmpty(request.getStyleColor())){
            return ResponseInfo.error("型体颜色不能为空!");
        }
        UserInfoUtil.UserInfo userInfo = UserInfoUtil.getUserInfo();
        if (!ObjectUtils.isEmpty(request.getMtlName())){
            String mtlColor="";
            if(ObjectUtils.isEmpty(request.getMtlColor())){
                String bomMtlColor = partMapper.getBomMtlColor(request);
                mtlColor=bomMtlColor;
            }else {
                mtlColor = request.getMtlColor();
            }
            partMapper.delYfSubMtlTemp2(userInfo.getUserName());
            List<YfSubMtlTemp2Dto> list = rAndDUsageMapper.getDetailsOfAdhesiveSubMaterials2(mtlColor,request.getMtlName(),request.getMtlSpec(),request.getMtlUnit());
            if (!ObjectUtils.isEmpty(list)){
                for (YfSubMtlTemp2Dto t : list) {
                    YfSubMtlTemp2 yfSubMtlTemp2 = new YfSubMtlTemp2();
                    BeanUtil.copyProperties(t,yfSubMtlTemp2);
                    if (!ObjectUtils.isEmpty(t.getMtlNo())){
                        if (t.getMtlNo().length() >= 7) {
                            String mtlNo = t.getMtlNo().substring(0,t.getMtlNo().length() - 5);
                            yfSubMtlTemp2.setMtlNo(mtlNo);
                        }
                    }
                    yfSubMtlTemp2.setCompanyId(userInfo.getCompanyId());
                    yfSubMtlTemp2.setSysUser(userInfo.getUserName());
                    partMapper.addCache(yfSubMtlTemp2);
                }
            }
        }else {
            partMapper.delYfSubMtlTemp2(userInfo.getUserName());
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 贴合材料还原
     * @param request
     * @return
     */
    @Override
    public ResponseInfo restoration2(AdhesiveMaterialChooseRequest request) {
        if (ObjectUtils.isEmpty(request.getStyleNo())){
            return ResponseInfo.error("型体不能为空!");
        }
        if (ObjectUtils.isEmpty(request.getStyleColor())){
            return ResponseInfo.error("型体颜色不能为空!");
        }
        UserInfoUtil.UserInfo userInfo = UserInfoUtil.getUserInfo();
        if (!ObjectUtils.isEmpty(request.getMtlName())){
            String mtlColor="";
            if(ObjectUtils.isEmpty(request.getMtlColor())){
                String bomMtlColor = partMapper.getBomMtlColor2(request);
                mtlColor=bomMtlColor;
            }else {
                mtlColor = request.getMtlColor();
            }
            partMapper.delYfSubMtlTemp2(userInfo.getUserName());
            List<YfSubMtlTemp2Dto> list = rAndDUsageMapper.getDetailsOfAdhesiveSubMaterials2(mtlColor,request.getMtlName(),request.getMtlSpec(),request.getMtlUnit());
            if (!ObjectUtils.isEmpty(list)){
                for (YfSubMtlTemp2Dto t : list) {
                    YfSubMtlTemp2 yfSubMtlTemp2 = new YfSubMtlTemp2();
                    BeanUtil.copyProperties(t,yfSubMtlTemp2);
                    if (!ObjectUtils.isEmpty(t.getMtlNo())){
                        if (t.getMtlNo().length() >= 7) {
                            String mtlNo = t.getMtlNo().substring(0,t.getMtlNo().length() - 5);
                            yfSubMtlTemp2.setMtlNo(mtlNo);
                        }
                    }
                    yfSubMtlTemp2.setCompanyId(userInfo.getCompanyId());
                    yfSubMtlTemp2.setSysUser(userInfo.getUserName());
                    partMapper.addCache(yfSubMtlTemp2);
                }
            }
        }else {
            partMapper.delYfSubMtlTemp2(userInfo.getUserName());
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 根据物料代码返回材料信息
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getMtlNoByMaterial(AdhesiveMaterialChooseRequest request) {
        if (ObjectUtils.isEmpty(request.getStyleNo())){
            return ResponseInfo.error("型体不能为空!");
        }
        if (ObjectUtils.isEmpty(request.getStyleColor())){
            return ResponseInfo.error("型体颜色不能为空!");
        }
        if (ObjectUtils.isEmpty(request.getPartName())) {
            return ResponseInfo.error("请输入部位!");
        }
        //查询部位是否存在!
        String partName = partMapper.getPartName(request.getPartName());
        if (StringUtils.isEmpty(partName)) {
            return ResponseInfo.error("部位输入错误!");
        }
        //查询物料编码是否存在
        YfBomMtlDto dto = partMapper.getMaterialStyleNo(request.getMtlNo());
        if (ObjectUtils.isEmpty(dto)){
            return ResponseInfo.error("物料编码输入错误!");
        }
        YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
        BeanUtil.copyProperties(dto, yfBomPartNew2);
        yfBomPartNew2.setPartName(request.getPartName());
        yfBomPartNew2.setSegment(String.valueOf(request.getSegment()));
        yfBomPartNew2.setStyleNo(request.getStyleNo());
        yfBomPartNew2.setStyleColor(request.getStyleColor());
        yfBomPartNew2.setSeqNo(request.getSeqNo());
        yfBomPartNew2.setMtlNo2(dto.getMtlNo());
        yfBomPartNew2.setCompanyId(UserIdThread.get().getCompanyId());
        YfMtlTypeNew detail = getTypeDetail(request.getMtlNo());
        if (!ObjectUtils.isEmpty(detail)){
            if ("冲裁".equals(detail.getDrawDept())){
                yfBomPartNew2.setDrawDept(detail.getDrawDept());
                yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
            }
            if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                yfBomPartNew2.setDrawDept(detail.getDrawDept());
                yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
            }
        }
        String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
        yfBomPartNew2.setUsageType(usageType);
        yfBomPartNew2.setSizeType("全号");
        if (!ObjectUtils.isEmpty(request.getSegment())){
            rAndDUsageMapper.UpdateStyleUsageDetailBySegment(yfBomPartNew2);
            getUsageType(yfBomPartNew2);
            return ResponseInfo.ok(yfBomPartNew2);
        }else {
            List<String> segment =getPositionSegment(request.getStyleNo(), request.getStyleColor(), request.getPartName());
            @NotNull String[] value = getValue(segment);
            if (value[1].equals(DefineEnum.CHECKOUT_FLAG.getValue())){
                return ResponseInfo.error("已到段添加上限!");
            }
            yfBomPartNew2.setSegment(value[0]);
            rAndDUsageMapper.addStyleUsageDetail2(yfBomPartNew2);
            getUsageType(yfBomPartNew2);
            return ResponseInfo.ok(yfBomPartNew2);
        }
    }

    /**
     * 根据物料代码返回材料信息
     * @param request
     * @return
     */
    @Override
    public ResponseInfo getMtlNoByMaterial2(AdhesiveMaterialChooseRequest request) {
        if (ObjectUtils.isEmpty(request.getStyleNo())){
            return ResponseInfo.error("型体不能为空!");
        }
        if (ObjectUtils.isEmpty(request.getStyleColor())){
            return ResponseInfo.error("型体颜色不能为空!");
        }
        if (ObjectUtils.isEmpty(request.getOrderId())){
            return ResponseInfo.error("订单号不能为空!");
        }
        if (ObjectUtils.isEmpty(request.getPartName())) {
            return ResponseInfo.error("请输入部位!");
        }
        //查询部位是否存在!
        String partName = partMapper.getPartName(request.getPartName());
        if (StringUtils.isEmpty(partName)) {
            return ResponseInfo.error("部位输入错误!");
        }
        //查询物料编码是否存在
        YfBomMtlDto dto = partMapper.getMaterialStyleNo(request.getMtlNo());
        if (ObjectUtils.isEmpty(dto)){
            return ResponseInfo.error("物料编码输入错误!");
        }
        YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
        BeanUtil.copyProperties(dto, yfBomPartNew2);
        yfBomPartNew2.setPartName(request.getPartName());
        yfBomPartNew2.setSegment(String.valueOf(request.getSegment()));
        yfBomPartNew2.setStyleNo(request.getStyleNo());
        yfBomPartNew2.setStyleColor(request.getStyleColor());
        yfBomPartNew2.setSeqNo(request.getSeqNo());
        yfBomPartNew2.setOrderId(request.getOrderId());
        yfBomPartNew2.setMtlNo2(dto.getMtlNo());
        yfBomPartNew2.setCompanyId(UserIdThread.get().getCompanyId());
        YfMtlTypeNew detail = getTypeDetail(request.getMtlNo());
        if (!ObjectUtils.isEmpty(detail)){
            if ("冲裁".equals(detail.getDrawDept())){
                yfBomPartNew2.setDrawDept(detail.getDrawDept());
                yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
            }
            if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                yfBomPartNew2.setDrawDept(detail.getDrawDept());
                yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
            }
        }
        String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
        yfBomPartNew2.setUsageType(usageType);
        yfBomPartNew2.setSizeType("全号");
        if (!ObjectUtils.isEmpty(request.getSegment())){
            developmentUsageMapper.UpdateStyleUsageDetailBySegment(yfBomPartNew2);
            getUsageType(yfBomPartNew2);
            return ResponseInfo.ok(yfBomPartNew2);
        }else {
            List<String> segment =getPositionSegment(request.getStyleNo(), request.getStyleColor(), request.getPartName());
            @NotNull String[] value = getValue(segment);
            if (value[1].equals(DefineEnum.CHECKOUT_FLAG.getValue())){
                return ResponseInfo.error("已到段添加上限!");
            }
            yfBomPartNew2.setSegment(value[0]);
            developmentUsageMapper.addStyleUsageDetail2(yfBomPartNew2);
            getUsageType(yfBomPartNew2);
            return ResponseInfo.ok(yfBomPartNew2);
        }
    }

    /**
     * 处理计算方式
     * @param yfBomPartNew2
     */
    private static void getUsageType(YfBomPartNew2 yfBomPartNew2) {
        if (!ObjectUtils.isEmpty(yfBomPartNew2.getUsageType())){
            switch (yfBomPartNew2.getUsageType()) {
                case "B":
                    yfBomPartNew2.setUsageType(DefineEnum.BASIC_CODE.getValue());
                    break;
                case "M":
                    yfBomPartNew2.setUsageType(DefineEnum.MIDDLE_CODE.getValue());
                    break;
                case "F":
                    yfBomPartNew2.setUsageType(DefineEnum.FULL_CODE.getValue());
                    break;
                default:
                    yfBomPartNew2.setUsageType(null);
            }
        }
    }


    /**
     * 根据物料名称返回信息
     * @param map
     * @return
     */
    @Override
    public ResponseInfo getMtlNameByMaterial(Map<String, String> map) {
        String mtlName = map.get("mtlName");
        String mtlSpec = map.get("mtlSpec");
        String mtlUnit = map.get("mtlUnit");
        YfBomMtlDto mtlNameByMaterial = partMapper.getMtlNameByMaterial(mtlName,mtlSpec,mtlUnit);
        return ResponseInfo.ok(mtlNameByMaterial);
    }

    /**
     * 删除物料代码
     * @param mtlNo
     * @return
     */
    @Override
    public ResponseInfo delMtlNoByMaterial(String mtlNo) {
        String bomByMtlNo = rAndDUsageMapper.getBomByMtlNo(mtlNo);
        if (!ObjectUtils.isEmpty(bomByMtlNo)){
            return ResponseInfo.error("型体BOM已使用,不能删除!");
        }

        //如果已经订购了，不能删除
        LotAndMtlRequest lotAndMtlRequest = new LotAndMtlRequest();
        lotAndMtlRequest.setMtlNo(mtlNo);
        List<PurchaseOrderDto> list = materialClearanceMapper.getPurchaseOrder(lotAndMtlRequest);
        if( com.alks.common.utils.beanutils.ObjectUtils.isNotEmpty(list)){
            throw new ServiceErrorException("已订购,不能删除!");
        }

        partMapper.delMtlNoByMaterial(mtlNo);
        partMapper.delMtlNoByMaterial2(mtlNo);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 查询部位名称
     * @param partName
     * @return
     */
    @Override
    public ResponseInfo getPartName(String partName) {
        return ResponseInfo.ok(partMapper.getPartName(partName));
    }

    @Autowired
    private DevelopmentUsageMapper developmentUsageMapper;

    /**
     * 选择当前行正式材料到用量表
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getChooseCurrentMaterials2(AdhesiveMaterialChooseRequest request) {

        if (ObjectUtils.isEmpty(request.getPartName())) {
            return ResponseInfo.error("请输入部位!");
        }

        //查询部位是否存在!
        String partName = partMapper.getPartName(request.getPartName());
        if (StringUtils.isEmpty(partName)) {
            return ResponseInfo.error("部位输入错误!");
        }

        //判断是否是贴合材料
        String cache = partMapper.getCacheToYfBomFitSub(request.getMtlNo());
        if (!ObjectUtils.isEmpty(cache)) {
            return ResponseInfo.error("贴合材料不能在此处理!");
        }

        String companyId = UserIdThread.get().getCompanyId();

        //如果段不为空
        if (!ObjectUtils.isEmpty(request.getSegment())) {
            YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
            BeanUtil.copyProperties(request, yfBomPartNew2);
            yfBomPartNew2.setMtlNo2(request.getMtlNo());

            YfMtlTypeNew detail = getTypeDetail(request.getMtlNo());
            if (!ObjectUtils.isEmpty(detail)){
                if ("冲裁".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
                if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
            }
            String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
            yfBomPartNew2.setUsageType(usageType);

            //修改
            developmentUsageMapper.UpdateStyleUsageDetailBySegment(yfBomPartNew2);
        } else {
            //查询型体资料颜色段
            List<String> segment = developmentUsageMapper.getPositionSegment(request.getStyleNo(), request.getStyleColor(), request.getPartName(),request.getOrderId());

            YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
            BeanUtil.copyProperties(request, yfBomPartNew2);

            //获取段
            @NotNull String[] value = getValue(segment);
            if (value[1].equals(DefineEnum.CHECKOUT_FLAG.getValue())){
                return ResponseInfo.error("已到段添加上限!");
            }

            yfBomPartNew2.setSegment(value[0]);
            yfBomPartNew2.setCompanyId(companyId);
            yfBomPartNew2.setMtlNo2(request.getMtlNo());

            YfMtlTypeNew detail = getTypeDetail(request.getMtlNo());
            if (!ObjectUtils.isEmpty(detail)){
                if ("冲裁".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
                if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
            }
            String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
            yfBomPartNew2.setUsageType(usageType);

            //保存
            developmentUsageMapper.addStyleUsageDetail2(yfBomPartNew2);
        }

        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 选择生产正式材料到用量表
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getChooseCompositeMaterials2(AdhesiveMaterialChooseRequest request) {

        if (ObjectUtils.isEmpty(request.getPartName())) {
            return ResponseInfo.error("请输入部位!");
        }

        //查询部位是否存在!
        String partName = partMapper.getPartName(request.getPartName());
        if (StringUtils.isEmpty(partName)) {
            return ResponseInfo.error("部位输入错误!");
        }
        if (ObjectUtils.isEmpty(request.getMtlName())){
            return ResponseInfo.error("材料名称为空!");
        }
        if (!ObjectUtils.isEmpty(request.getMtlColor())){
            ColorSystemDto yfColorDetail = colorAttributeMapper.getYfColorDetail(request.getMtlColor());
            if (ObjectUtils.isEmpty(yfColorDetail)){
                return ResponseInfo.error("颜色输入错误!");
            }else {
                request.setColorCode(yfColorDetail.getColorCode());
            }
        }

        YfBomMtl yfBomMtl = new YfBomMtl();
        BeanUtil.copyProperties(request, yfBomMtl);

        String mtlNo = null;
        if (!ObjectUtils.isEmpty(request.getMtlNo())) {
            if (request.getMtlNo().length() < Integer.parseInt(DefineEnum.FOURTEEN.getValue())) {
                if (!ObjectUtils.isEmpty(request.getMtlColor()) && !ObjectUtils.isEmpty(request.getColorCode())) {
                    mtlNo = request.getMtlNo() + request.getColorCode();
                    yfBomMtl.setMtlNo(mtlNo);
                } else {
                    mtlNo = request.getMtlNo() + DefineEnum.FIVE_ZERO.getValue();
                    yfBomMtl.setMtlNo(mtlNo);
                }
            }
        }

        String companyId = UserIdThread.get().getCompanyId();

        //查询物料编码是否存在
        YfBomMtlDto materialStyleNo = partMapper.getMaterialStyleNo(yfBomMtl.getMtlNo());
        if (ObjectUtils.isEmpty(materialStyleNo)) {
            yfBomMtl.setCompanyId(companyId);
            yfBomMtl.setSysUser(UserIdThread.get().getUserName());
            yfBomMtl.setSysDate(new Date());

            //添加
            partMapper.addCompositeMaterials(yfBomMtl);
        }else {
            if (!materialStyleNo.getMtlName().equals(yfBomMtl.getMtlName())) {
                partMapper.updateYfBomMtl(yfBomMtl.getMtlNo(), yfBomMtl.getMtlName());
            }
        }

        //如果段不为空
        if (!ObjectUtils.isEmpty(request.getSegment())) {
            YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
            BeanUtil.copyProperties(request, yfBomPartNew2);
            yfBomPartNew2.setMtlNo(mtlNo);
            yfBomPartNew2.setMtlNo2(mtlNo);

            YfMtlTypeNew detail = getTypeDetail(mtlNo);
            if (!ObjectUtils.isEmpty(detail)){
                if ("冲裁".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
                if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
            }
            String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
            yfBomPartNew2.setUsageType(usageType);
            yfBomPartNew2.setSizeType("全号");

            //修改
            developmentUsageMapper.UpdateStyleUsageDetailBySegment(yfBomPartNew2);
        } else {

            //查询型体资料颜色段
            List<String> segment = developmentUsageMapper.getPositionSegment(request.getStyleNo(), request.getStyleColor(), request.getPartName(),request.getOrderId());

            YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
            BeanUtil.copyProperties(request, yfBomPartNew2);

            //获取段
            @NotNull String[] value = getValue(segment);
            if (value[1].equals(DefineEnum.CHECKOUT_FLAG.getValue())){
                return ResponseInfo.error("已到段添加上限!");
            }

            yfBomPartNew2.setSegment(value[0]);
            yfBomPartNew2.setCompanyId(companyId);
            yfBomPartNew2.setMtlNo(mtlNo);
            yfBomPartNew2.setMtlNo2(mtlNo);

            YfMtlTypeNew detail = getTypeDetail(mtlNo);
            if (!ObjectUtils.isEmpty(detail)){
                if ("冲裁".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
                if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
            }
            String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
            yfBomPartNew2.setUsageType(usageType);
            yfBomPartNew2.setSizeType("全号");

            //保存
            developmentUsageMapper.addStyleUsageDetail2(yfBomPartNew2);
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 贴合材料处理-插入材料
     * @param request@return
     */
    @Override
    @Transactional
    public ResponseInfo getInsertMaterial2(AdhesiveMaterialChooseRequest request) {
        if (ObjectUtils.isEmpty(request.getPartName())) {
            return ResponseInfo.error("请输入部位!");
        }

        //查询部位是否存在!
        String partName = partMapper.getPartName(request.getPartName());
        if (StringUtils.isEmpty(partName)) {
            return ResponseInfo.error("部位输入错误!");
        }

        //判断是否是贴合材料
        String cache = partMapper.getCacheToYfBomFitSub(request.getMtlNo());
        if (ObjectUtils.isEmpty(cache)) {
            return ResponseInfo.error("非贴合材料不能插入!");
        }

        String companyId = UserIdThread.get().getCompanyId();

        //如果段不为空
        if (!ObjectUtils.isEmpty(request.getSegment())) {
            YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
            BeanUtil.copyProperties(request, yfBomPartNew2);
            yfBomPartNew2.setMtlNo2(request.getMtlNo());

            YfMtlTypeNew detail = getTypeDetail(request.getMtlNo());
            if (!ObjectUtils.isEmpty(detail)){
                if ("冲裁".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
                if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
            }
            String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
            yfBomPartNew2.setUsageType(usageType);
            yfBomPartNew2.setSizeType("全号");

            //修改
            developmentUsageMapper.UpdateStyleUsageDetailBySegment(yfBomPartNew2);
        } else {
            //查询型体资料颜色段
            List<String> segment = developmentUsageMapper.getPositionSegment(request.getStyleNo(), request.getStyleColor(), request.getPartName(),request.getOrderId());

            YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
            BeanUtil.copyProperties(request, yfBomPartNew2);

            //获取段
            @NotNull String[] value = getValue(segment);
            if (value[1].equals(DefineEnum.CHECKOUT_FLAG.getValue())){
                return ResponseInfo.error("已到段添加上限!");
            }

            //设置段
            yfBomPartNew2.setSegment(value[0]);
            yfBomPartNew2.setCompanyId(companyId);
            yfBomPartNew2.setMtlNo2(request.getMtlNo());

            YfMtlTypeNew detail = getTypeDetail(request.getMtlNo());
            if (!ObjectUtils.isEmpty(detail)){
                if ("冲裁".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
                if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                    yfBomPartNew2.setDrawDept(detail.getDrawDept());
                    yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                }
            }
            String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
            yfBomPartNew2.setUsageType(usageType);
            yfBomPartNew2.setSizeType("全号");
            //保存
            developmentUsageMapper.addStyleUsageDetail2(yfBomPartNew2);
        }

        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 贴合材料插入
     * @param request
     * @return
     */
    @Override
    @Transactional
    public ResponseInfo getCompositeMaterialsAdd2(AdhesiveMaterialAddRequest request) {
        if (request == null) {
            return ResponseInfo.error("请求参数不能为空!");
        }
        SysUserTokenDTO dto = UserIdThread.get();
        String userName = dto.getUserName();
        String companyId = dto.getCompanyId();
        //贴合材料编码
        String numCode = null;
        if (!ObjectUtils.isEmpty(request.getCacheVoRequests())) {
            if (request.getCacheVoRequests().size() == 1) {
                YfBomMtl yfBomMtl = new YfBomMtl();
                BeanUtil.copyProperties(request, yfBomMtl);
                yfBomMtl.setSysUser(userName);
                yfBomMtl.setCompanyId(companyId);
                yfBomMtl.setSysDate(new Date());

                if (!ObjectUtils.isEmpty(request.getMtlColor())){
                    ColorSystemDto yfColorDetail = colorAttributeMapper.getYfColorDetail(request.getMtlColor());
                    if (ObjectUtils.isEmpty(yfColorDetail)){
                        return ResponseInfo.error("颜色输入错误!");
                    }else {
                        request.setColorCode(yfColorDetail.getColorCode());
                    }
                }

                String mtlNo = null;
                //如果颜色编码不为空拼接颜色编码
                if (request.getMtlNo().length() < Integer.parseInt(DefineEnum.FOURTEEN.getValue())) {
                    if (!StringUtils.isEmpty(request.getColorCode()) && !StringUtils.isEmpty(request.getMtlNo())) {
                        mtlNo = request.getMtlNo() + request.getColorCode();
                        yfBomMtl.setMtlNo(mtlNo);
                    } else {
                        mtlNo = request.getMtlNo() + DefineEnum.FIVE_ZERO.getValue();
                        yfBomMtl.setMtlNo(mtlNo);
                    }
                } else {
                    yfBomMtl.setMtlNo(request.getMtlNo());
                }
                numCode = yfBomMtl.getMtlNo();
                //判断加工方式
                if (ObjectUtils.isEmpty(request.getCacheVoRequests().get(0).getPrdType())) {
                    YfBomMtlDto mtlDto = partMapper.getYfBomMtl(yfBomMtl.getMtlColor(), yfBomMtl.getMtlName(), yfBomMtl.getMtlSpec(), yfBomMtl.getMtlUnit());
                    if (ObjectUtils.isEmpty(mtlDto)) {
                        YfBomMtlDto materialStyleNo = partMapper.getMaterialStyleNo(yfBomMtl.getMtlNo());
                        if (ObjectUtils.isEmpty(materialStyleNo)) {
                            //添加
                            partMapper.addCompositeMaterials(yfBomMtl);
                        }
                        getBomMaterials2(request, companyId, numCode);
                    }else {
                        getBomMaterials2(request, companyId, mtlDto.getMtlNo());
                    }
                }else {
                    numCode = partMapper.getMaterialMtlNo();
                    String finalNumCode = numCode;
                    yfBomMtl.setMtlNo(numCode);
                    YfBomMtlDto mtlDto = partMapper.getYfBomMtl(yfBomMtl.getMtlColor(), yfBomMtl.getMtlName(), yfBomMtl.getMtlSpec(), yfBomMtl.getMtlUnit());
                    if (ObjectUtils.isEmpty(mtlDto)) {
                        YfBomMtlDto materialStyleNo = partMapper.getMaterialStyleNo(yfBomMtl.getMtlNo());
                        if (ObjectUtils.isEmpty(materialStyleNo)) {
                            //添加
                            partMapper.addCompositeMaterials(yfBomMtl);
                            List<YfBomFitSub> list = request.getCacheVoRequests().stream().map(s -> {
                                YfBomFitSub yfBomFitSub = new YfBomFitSub();
                                BeanUtil.copyProperties(request, yfBomFitSub);
                                String mtlNo2 = null;

                                if (!ObjectUtils.isEmpty(s.getMtlColor())){
                                    ColorSystemDto yfColorDetail = colorAttributeMapper.getYfColorDetail(s.getMtlColor());
                                    if (!ObjectUtils.isEmpty(yfColorDetail)){
                                        s.setColorCode(yfColorDetail.getColorCode());
                                    }
                                }

                                //如果颜色编码不为空拼接颜色编码
                                if (s.getMtlNo().length() < Integer.parseInt(DefineEnum.FOURTEEN.getValue())) {
                                    if (!StringUtils.isEmpty(s.getColorCode()) && !StringUtils.isEmpty(s.getMtlNo())) {
                                        mtlNo2 = s.getMtlNo() + s.getColorCode();
                                        yfBomFitSub.setSubMtlNo(mtlNo2);
                                    } else {
                                        mtlNo2 = s.getMtlNo() + DefineEnum.FIVE_ZERO.getValue();
                                        yfBomFitSub.setSubMtlNo(mtlNo2);
                                    }
                                } else {
                                    yfBomFitSub.setSubMtlNo(s.getMtlNo());
                                }
                                yfBomFitSub.setCompanyId(companyId);
                                yfBomFitSub.setMtlNo(finalNumCode);
                                yfBomFitSub.setSysUser(userName);
                                yfBomFitSub.setSubMtlName(s.getMtlName());
                                yfBomFitSub.setSubMtlColor(s.getMtlColor());
                                yfBomFitSub.setSubMtlSpec(s.getMtlSpec());
                                yfBomFitSub.setSubMtlUnit(s.getMtlUnit());
                                yfBomFitSub.setPrdType(s.getPrdType());
                                yfBomFitSub.setPercent(s.getPercent());
                                yfBomFitSub.setPurFlag(s.getPurFlag());
                                yfBomFitSub.setMtlSeq(s.getMtlSeq());
                                yfBomFitSub.setVendor(s.getVendor());
                                return yfBomFitSub;
                            }).collect(Collectors.toList());
                            partMapper.addCacheToYfBomFitSub(list);
                        }
                        getBomMaterials2(request, companyId, numCode);
                    }else {
                        getBomMaterials2(request, companyId, mtlDto.getMtlNo());
                    }
                }
            }else {
                numCode = partMapper.getMaterialMtlNo();
                String finalNumCode = numCode;
                YfBomMtl bomMtl = new YfBomMtl();
                BeanUtil.copyProperties(request, bomMtl);
                bomMtl.setMtlNo(finalNumCode);
                bomMtl.setCompanyId(companyId);
                bomMtl.setSysUser(userName);
                bomMtl.setSysDate(new Date());
                YfBomMtlDto yfBomMtlDto = partMapper.getYfBomMtl(bomMtl.getMtlColor(), bomMtl.getMtlName(), bomMtl.getMtlSpec(), bomMtl.getMtlUnit());
                if (ObjectUtils.isEmpty(yfBomMtlDto)) {
                    YfBomMtlDto materialStyleNo = partMapper.getMaterialStyleNo(bomMtl.getMtlNo());
                    if (ObjectUtils.isEmpty(materialStyleNo)) {
                        //添加
                        partMapper.addCompositeMaterials(bomMtl);
                        List<YfBomFitSub> list = request.getCacheVoRequests().stream().map(s -> {
                            YfBomFitSub yfBomFitSub = new YfBomFitSub();
                            BeanUtil.copyProperties(request, yfBomFitSub);

                            String mtlNo = null;

                            if (!ObjectUtils.isEmpty(s.getMtlColor())){
                                ColorSystemDto yfColorDetail = colorAttributeMapper.getYfColorDetail(s.getMtlColor());
                                if (!ObjectUtils.isEmpty(yfColorDetail)){
                                    s.setColorCode(yfColorDetail.getColorCode());
                                }
                            }
                            //如果颜色编码不为空拼接颜色编码
                            if (s.getMtlNo().length() < Integer.parseInt(DefineEnum.FOURTEEN.getValue())) {
                                if (!StringUtils.isEmpty(s.getColorCode()) && !StringUtils.isEmpty(s.getMtlNo())) {
                                    mtlNo = s.getMtlNo() + s.getColorCode();
                                    yfBomFitSub.setSubMtlNo(mtlNo);
                                } else {
                                    mtlNo = s.getMtlNo() + DefineEnum.FIVE_ZERO.getValue();
                                    yfBomFitSub.setSubMtlNo(mtlNo);
                                }
                            } else {
                                mtlNo=s.getMtlNo();
                                yfBomFitSub.setSubMtlNo(s.getMtlNo());
                            }

                            //查询物料编码是否存在
                            YfBomMtlDto bomMtlDto = partMapper.getMaterialStyleNo(yfBomFitSub.getSubMtlNo());
                            YfBomMtlDto mtlDto = partMapper.getYfBomMtl(s.getMtlColor(), s.getMtlName(), s.getMtlSpec(),s.getMtlUnit());
                            if (ObjectUtils.isEmpty(mtlDto)) {
                                if (ObjectUtils.isEmpty(bomMtlDto)) {
                                    YfBomMtl bomMtl2 = new YfBomMtl();
                                    BeanUtil.copyProperties(s, bomMtl2);
                                    bomMtl2.setMtlNo(mtlNo);
                                    bomMtl2.setCompanyId(companyId);
                                    bomMtl2.setSysUser(userName);
                                    bomMtl2.setSysDate(new Date());
                                    //添加
                                    partMapper.addCompositeMaterials(bomMtl2);
                                }
                            }
                            yfBomFitSub.setCompanyId(companyId);
                            yfBomFitSub.setMtlNo(finalNumCode);
                            yfBomFitSub.setSysUser(userName);
                            yfBomFitSub.setSubMtlName(s.getMtlName());
                            yfBomFitSub.setSubMtlColor(s.getMtlColor());
                            yfBomFitSub.setSubMtlSpec(s.getMtlSpec());
                            yfBomFitSub.setSubMtlUnit(s.getMtlUnit());
                            yfBomFitSub.setPrdType(s.getPrdType());
                            yfBomFitSub.setPercent(s.getPercent());
                            yfBomFitSub.setPurFlag(s.getPurFlag());
                            yfBomFitSub.setMtlSeq(s.getMtlSeq());
                            yfBomFitSub.setVendor(s.getVendor());
                            return yfBomFitSub;
                        }).collect(Collectors.toList());
                        partMapper.addCacheToYfBomFitSub(list);
                    }
                    getBomMaterials2(request, companyId, numCode);
                }else {
                    getBomMaterials2(request, companyId, yfBomMtlDto.getMtlNo());
                }
            }
        }

        //删除
        partMapper.delCache(request.getIds(), userName);
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * bom资料数据
     * @param request
     * @param companyId
     * @param numCode
     */
    @Nullable
    private void getBomMaterials2(AdhesiveMaterialAddRequest request, String companyId, String numCode) {
        //判断partList是否存在
        if (!ObjectUtils.isEmpty(request.getPartList())){
            if (ObjectUtils.isEmpty(request.getPartList().getPartName())){
                throw new ServiceErrorException("请输入部位!");
            }
            //查询部位是否存在!
            String partName = partMapper.getPartName(request.getPartList().getPartName());
            if (StringUtils.isEmpty(partName)) {
                throw new ServiceErrorException("部位输入错误!");
            }

            //判断段是否存在
            if (!ObjectUtils.isEmpty(request.getPartList().getSegment())){
                YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
                BeanUtil.copyProperties(request, yfBomPartNew2);
                yfBomPartNew2.setMtlNo(numCode);
                yfBomPartNew2.setMtlNo2(numCode);
                yfBomPartNew2.setStyleNo(request.getPartList().getStyleNo());
                yfBomPartNew2.setStyleColor(request.getPartList().getStyleColor());
                yfBomPartNew2.setPartName(request.getPartList().getPartName());
                yfBomPartNew2.setSeqNo(request.getPartList().getSeqNo());
                yfBomPartNew2.setSegment(request.getPartList().getSegment());
                yfBomPartNew2.setOrderId(request.getPartList().getOrderId());
                YfMtlTypeNew detail = getTypeDetail(numCode);
                if (!ObjectUtils.isEmpty(detail)){
                    if ("冲裁".equals(detail.getDrawDept())){
                        yfBomPartNew2.setDrawDept(detail.getDrawDept());
                        yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                    }
                    if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                        yfBomPartNew2.setDrawDept(detail.getDrawDept());
                        yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                    }
                }
                String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
                yfBomPartNew2.setUsageType(usageType);
                yfBomPartNew2.setSizeType("全号");
                //修改
                developmentUsageMapper.UpdateStyleUsageDetailBySegment(yfBomPartNew2);
            }else {
                //查询型体资料颜色段
                List<String> segment = developmentUsageMapper.getPositionSegment(request.getPartList().getStyleNo(), request.getPartList().getStyleColor(), request.getPartList().getPartName(),request.getPartList().getOrderId());
                YfBomPartNew2 yfBomPartNew2 = new YfBomPartNew2();
                BeanUtil.copyProperties(request, yfBomPartNew2);
                //获取段
                @NotNull String[] value = getValue(segment);
                if (value[1].equals(DefineEnum.CHECKOUT_FLAG.getValue())){
                    throw new ServiceErrorException("已到段添加上限!");
                }
                yfBomPartNew2.setSegment(value[0]);
                yfBomPartNew2.setCompanyId(companyId);
                yfBomPartNew2.setMtlNo(numCode);
                yfBomPartNew2.setMtlNo2(numCode);
                yfBomPartNew2.setStyleNo(request.getPartList().getStyleNo());
                yfBomPartNew2.setStyleColor(request.getPartList().getStyleColor());
                yfBomPartNew2.setPartName(request.getPartList().getPartName());
                yfBomPartNew2.setSeqNo(request.getPartList().getSeqNo());
                yfBomPartNew2.setOrderId(request.getPartList().getOrderId());
                YfMtlTypeNew detail = getTypeDetail(numCode);
                if (!ObjectUtils.isEmpty(detail)){
                    if ("冲裁".equals(detail.getDrawDept())){
                        yfBomPartNew2.setDrawDept(detail.getDrawDept());
                        yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                    }
                    if ("针车".equals(detail.getDrawDept())||"成型".equals(detail.getDrawDept())){
                        yfBomPartNew2.setDrawDept(detail.getDrawDept());
                        yfBomPartNew2.setMtlProperty(detail.getMtlProperty());
                    }
                }
                String usageType = getUsageType(yfBomPartNew2.getMtlUnit());
                yfBomPartNew2.setUsageType(usageType);
                yfBomPartNew2.setSizeType("全号");
                //保存
                developmentUsageMapper.addStyleUsageDetail2(yfBomPartNew2);
            }
        }
    }

    /**
     * 0 未审核 1审核 2 退回
     * @param request
     * @param choose
     * @return
     */
    @Override
    public ResponseInfo check(List<MaterialQueryRequest> request, Integer choose) {
        if (ObjectUtils.isEmpty(request)){
            return ResponseInfo.error("请选择记录!");
        }
        switch (choose){
            case 0:
                int count = CheckCount(request, "0");
                if (count ==request.size()){
                    return ResponseInfo.error("存在"+count+"条记录未审核,请选择已审核的记录!");
                }
                partMapper.updateCheck(request,"0");
                break;
            case 1:
                int count2 = CheckCount(request, "1");
                if (count2 ==request.size()){
                    return ResponseInfo.error("存在"+count2+"条记录已审核,请选择未审核的记录!");
                }
                partMapper.updateCheck(request,"1");
                break;
            case 2:
                int count3 = CheckCount(request, "1");
                if (count3 ==request.size()){
                    return ResponseInfo.error("存在"+count3+"条记录已审核,不可退回!");
                }
                partMapper.updateCheck(request,"2");
                break;
            default:
                return ResponseInfo.error("请选择正确的操作!");
        }
        return ResponseInfo.ok("操作成功!");
    }

    /**
     * 0 未审核 1审核 2 退回
     * @param request
     * @param choose
     * @return
     */
    private int CheckCount(List<MaterialQueryRequest> request,String choose){
        int count = 0;
        for (MaterialQueryRequest t : request) {
            if (choose.equals(t.getStatus())) {
                count++;
            }
        }
        return count;
    }
}
